package cn.youqu.happy.module.youqu.controller.admin.userComment;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.annotations.*;

import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.youqu.happy.framework.common.pojo.PageResult;
import cn.youqu.happy.framework.common.pojo.CommonResult;
import static cn.youqu.happy.framework.common.pojo.CommonResult.success;

import cn.youqu.happy.framework.excel.core.util.ExcelUtils;

import cn.youqu.happy.framework.operatelog.core.annotations.OperateLog;
import static cn.youqu.happy.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.youqu.happy.module.youqu.controller.admin.userComment.vo.*;
import cn.youqu.happy.module.youqu.dal.dataobject.userComment.UserCommentDO;
import cn.youqu.happy.module.youqu.convert.userComment.UserCommentConvert;
import cn.youqu.happy.module.youqu.service.userComment.UserCommentService;

@Api(tags = "管理后台 - 用户回复")
@RestController
@RequestMapping("/youqu/user-comment")
@Validated
public class UserCommentController {

    @Resource
    private UserCommentService userCommentService;

    @PostMapping("/create")
    @ApiOperation("创建用户回复")
    @PreAuthorize("@ss.hasPermission('youqu:user-comment:create')")
    public CommonResult<Integer> createUserComment(@Valid @RequestBody UserCommentCreateReqVO createReqVO) {
        return success(userCommentService.publishUserComment(createReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新用户回复")
    @PreAuthorize("@ss.hasPermission('youqu:user-comment:update')")
    public CommonResult<Boolean> updateUserComment(@Valid @RequestBody UserCommentUpdateReqVO updateReqVO) {
        userCommentService.updateUserComment(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除用户回复")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = Integer.class)
    @PreAuthorize("@ss.hasPermission('youqu:user-comment:delete')")
    public CommonResult<Boolean> deleteUserComment(@RequestParam("id") Integer id) {
        userCommentService.deleteUserComment(id);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得用户回复")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = Integer.class)
    @PreAuthorize("@ss.hasPermission('youqu:user-comment:query')")
    public CommonResult<UserCommentRespVO> getUserComment(@RequestParam("id") Integer id) {
        UserCommentDO userComment = userCommentService.getUserComment(id);
        return success(UserCommentConvert.INSTANCE.convert(userComment));
    }

    @GetMapping("/list")
    @ApiOperation("获得用户回复列表")
    @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
    @PreAuthorize("@ss.hasPermission('youqu:user-comment:query')")
    public CommonResult<List<UserCommentRespVO>> getUserCommentList(@RequestParam("ids") Collection<Integer> ids) {
        List<UserCommentDO> list = userCommentService.getUserCommentList(ids);
        return success(UserCommentConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @ApiOperation("获得用户回复分页")
    @PreAuthorize("@ss.hasPermission('youqu:user-comment:query')")
    public CommonResult<PageResult<UserCommentRespVO>> getUserCommentPage(@Valid UserCommentPageReqVO pageVO) {
        PageResult<UserCommentDO> pageResult = userCommentService.getUserCommentPage(pageVO);
        return success(UserCommentConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @ApiOperation("导出用户回复 Excel")
    @PreAuthorize("@ss.hasPermission('youqu:user-comment:export')")
    @OperateLog(type = EXPORT)
    public void exportUserCommentExcel(@Valid UserCommentExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<UserCommentDO> list = userCommentService.getUserCommentList(exportReqVO);
        // 导出 Excel
        List<UserCommentExcelVO> datas = UserCommentConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "用户回复.xls", "数据", UserCommentExcelVO.class, datas);
    }

}
